package cn.anycase.demo.controller;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import cn.anycase.demo.vo.SampleUpdateInputVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.plugins.Page;
import cn.anycase.demo.base.Response;
import cn.anycase.demo.entity.ThisIsSample;
import cn.anycase.demo.service.ThisIsSampleService;
import cn.anycase.demo.vo.SampleQueryInputVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * <p>
 * </p>
 *
 * @author yuxiaobin
 * @date 2022/8/31
 */
@SuppressWarnings("deprecation")
@Slf4j
@Api(value = "SampleController", description = "Restful示例", tags = "示例")
@RestController
@RequestMapping("/sample")
public class SampleController {

    @Autowired
    ThisIsSampleService thisIsSampleService;
    @Autowired
    HttpServletRequest request;

    @ApiOperation(value = "创建sample记录", notes = "创建sample记录")
    @PostMapping("/createSampe")
    public Response create(@RequestBody ThisIsSample param) {
        log.info("接口 create 接收的入参:{}",param);
        Response response = thisIsSampleService.createRecord(param);
        log.info("接口 create 返回的参数:{}",response);
        return response;
    }

    protected <T> Page<T> getPage() {
        String pageStr = request.getParameter("page");
        String sizeStr = request.getParameter("size");
        int page = 1;
        int size = 10;
        if (!StringUtils.isEmpty(pageStr)) {
            try {
                page = Integer.parseInt(pageStr);
            } catch (Exception e) {
                //ignored
            }
        }
        if (!StringUtils.isEmpty(sizeStr)) {
            try {
                size = Integer.parseInt(sizeStr);
            } catch (Exception e) {
                //ignored
            }
        }
        return new Page<>(page, size);
    }

    @ApiOperation(value = "查询sample记录", notes = "查询sample记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "分页当前页,不传默认第一页", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "size", value = "分页一页多少条记录", dataType = "String", paramType = "query")})
    @GetMapping("/getSampleList")
    public Response list(SampleQueryInputVO param) {
        log.info("接口list 接收的参数param:{}",param);
        Page<ThisIsSample> pageList = thisIsSampleService.pageList(getPage(), param);
        return Response.success(pageList);
    }

    @ApiOperation(value = "更新sample记录", notes = "更新sample记录")
    @PutMapping("/updateSample")
    public Response update( @Valid @RequestBody SampleUpdateInputVO updateInputVO) {
        log.info("接口update的入参:{}",updateInputVO);
        Response response = thisIsSampleService.updateRecord(updateInputVO);
        return response;
    }


//
//    @ApiOperation(value = "查看某一条具体记录", notes = "查看某一条具体记录")
//    @GetMapping("/{id}")
//    public Response getDetail(@PathVariable("id") String id) {
//        return Response.success(thisIsSampleMapper.selectById(id));
//    }
//
//    @ApiOperation(value = "删除一条记录", notes = "删除一条记录")
//    @DeleteMapping("/{id}")
//    public Response deleteById(@PathVariable("id") String id) {
//        thisIsSampleMapper.deleteById(id);
//        return Response.success();
//    }
}
